﻿using System;

namespace FunctionalCLR.Core.Function.Flow {
    public class ForNode : FuncNode {

        public ForNode()
            : base() {
            base.Value = "for";
        }

        public override FuncNode GetReversedFunction(int index) {
            throw new NotImplementedException();
        }

        public override object Evaluate() {
            Children[Children.Count - 1].Evaluate(); // init
            string cond = Children[Children.Count - 2].Evaluate().ToString();
            string result = string.Empty;

            while (cond.ToLower().Equals("true")) {
                try {
                    Children[Children.Count - 4].Evaluate(); // loop body
                    Children[Children.Count - 3].Evaluate(); // step
                    cond = Children[Children.Count - 2].Evaluate().ToString();
                } catch (BreakException ex) {
                    break;
                }
            }

            return result;
        }
    }
}
